//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<helper.h>
///	@path	~/src/lib/utilities/
///	@date	2007/09/08
///	@desc	some helper class.

#pragma once

#include "xeres.h"

#include "lib/utilities/callback.h"

namespace xeres
{
	/*! 
		\brief	Initialize a variable by assigning specific value.
				It's used in global variable assignment.
	*/
	template<
		typename _Ty
	> struct ValueInitor
	{
		/// \ctor
		ValueInitor( _Ty& left , const _Ty& rhs )
		{
			left = rhs;
		}
	};

	/*! 
		\brief	Guards with different policy.
	*/
	template<
		typename _policy
	> struct Guard;

	/*! 
		\brief	Specialization of guard.
	*/
	template<> struct Guard<_call>
	{
		/// \ctor
		Guard( function<void()> func )
			: m_func( func )
		{
		}

		/// \dtor
		~Guard( void )
		{
			if( m_func )
				m_func();
		}

	private:

		function<void()>	m_func;
	};

	/*! 
		\brief	Specialization of free.
	*/
	template<> struct Guard<_free>
	{
		/// \ctor
		Guard( void * ptr )
			: m_ptr( ptr )
		{
		}

		/// \dtor
		~Guard( void )
		{
			if( m_ptr )
				free( m_ptr );
		}

	private:

		void *		m_ptr;
	};

} // namespace xeres
